﻿using BookManege.entity;
using BookManege.service;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace BookManege.controller
{
    public partial class stillBook : Form
    {
        private BorrowManegeService borrowManegeService;
        public stillBook()
        {
            InitializeComponent();
            borrowManegeService = new BorrowManegeService();
        }
        private void stillBook_Load(object sender, EventArgs e)
        {
            comboBox1.DataSource = borrowManegeService.BorrowList();
            comboBox1.DisplayMember = "book_name";
            comboBox1.ValueMember = "user_id";
        }
        //扫描
        private void button2_Click(object sender, EventArgs e)
        {
            StillBook stillBook = borrowManegeService.Search(textBox1.Text, 0);
            GetBorrowBook(stillBook);
        }
        //还书
        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认要归还《" + label3.Text + "》吗", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {

                if (borrowManegeService.StillBook(Convert.ToInt64(label12.Text)) == 250)
                {
                    MessageBox.Show("您已经提交过申请了！");
                }
                else
                {
                    if (Convert.ToInt32(label5.Text) > 1)
                    {
                        MessageBox.Show("您同时借阅了本书数量大于1，请同时归还全部！");
                    }
                    MessageBox.Show("您已提交归还申请！待管理员核对");
                }
                StillBook stillBook = borrowManegeService.Search(textBox1.Text, 0);
                GetBorrowBook(stillBook);
            }
        }
        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            int borrowId = Convert.ToInt32(comboBox1.SelectedValue);
            StillBook stillBook = borrowManegeService.Search(null, borrowId);
            GetBorrowBook(stillBook);
        }
        private void GetBorrowBook(StillBook stillBook)
        {
            if (stillBook != null)
            {
                button1.Enabled = true;
                panel1.Visible = true;
                label4.Visible = false;
                label12.Text = stillBook.Id.ToString();
                label3.Text = stillBook.BookName.ToString();
                label7.Text = stillBook.StartDate.ToString();
                label9.Text = stillBook.Exceed > 0 ? "还有" + stillBook.Exceed + "天" : "已逾期";
                label5.Text = stillBook.Number.ToString();
                label3.Tag = stillBook.BookId.ToString();
                if(stillBook.IsSubmit != 0)
                {
                    label14.Visible = true;
                }
                else
                {
                    label14.Visible = false;
                }
            }
            else
            {
                panel1.Visible = false;
                label4.Visible = true;
                button1.Enabled = false;
            }
        }
    }
}
